<template>
  <div>
    <el-dialog
      close="roll-dialog"
      v-model="dialogVisible"
      title="Tips"
      width="50%"
      :before-close="handleClose"
    >
      <el-form :model="ruleForm" ref="ruleFormRef">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="ruleForm.title" />
        </el-form-item>
        <el-form-item label="文章简述" prop="desc">
          <el-input v-model="ruleForm.desc" />
        </el-form-item>
        <el-form-item label="文章类型" prop="articleCategory">
          <el-radio-group v-model="ruleForm.articleCategory">
            <el-radio :label="0" >0-景点介绍</el-radio>
            <el-radio :label="1" >1-景点游记</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="文章图片" prop="titleImageUrl">
          <el-input v-model="ruleForm.titleImageUrl" />
        </el-form-item>
        <el-form-item>
          <Editor v-model="ruleForm.content" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">Cancel</el-button>
          <el-button
            type="primary"
            @click="updateArticle((dialogVisible = false))"
            >提交</el-button
          >
        </span>
      </template>
    </el-dialog>

    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i>
          文章信息
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input
          v-model="query.title"
          placeholder="文章名称"
          class="handle-input mr10"
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch"
          >搜索</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
      >
        <el-table-column
          prop="articleId"
          label="文章ID"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column label="文章图片" width="180">
                    <template #default="scope">
                        <el-image :src="scope.row.titleImageUrl"></el-image>
                    </template>
                </el-table-column>
        <el-table-column
          prop="title"
          label="文章名称"
          width="80"
        ></el-table-column>
        <el-table-column label="文章类型" width="100" align="center">
          <template #default="scope">
            <el-tag
              :type="
                scope.row.type == '0'
                  ? 'danger'
                  : scope.row.type == '1'
                  ? 'success'
                  : 'warning'
              "
            >
              {{
                scope.row.articleCategory == "0"
                  ? "小说"
                  : scope.row.articleCategory == "1"
                  ? "名著"
                  : "传记"
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="desc" label="文章简述"></el-table-column>
        <el-table-column prop="author" label="文章作者"></el-table-column>
        <el-table-column prop="createdAt" label="注册时间"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          v-if="totalCount"
          layout="total, prev, pager, next"
          :current-page="query.currentPage"
          :page-size="query.pageSize"
          :total="totalCount"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { ref, reactive, onMounted } from "vue";
import { ElMessageBox } from "element-plus";
import request from "../../utils/request";
import Editor from "@/components/Editor.vue";
import { ElNotification } from "element-plus";
export default {
  components: {
    Editor,
  },
  setup() {
    const tableData = ref([]);
    const totalCount = ref(0);
    const dialogVisible = ref(false);
    const ruleFormRef = ref();
    const ruleForm = reactive({
      articleId: 0,
      content: "",
      title: "",
      desc: "",
      articleCategory: 0,
      titleImageUrl: "",
    });
    const query = reactive({
      title: "",
      currentPage: 1,
      pageSize: 5,
    });

    // 查询操作
    const handleSearch = () => {
      getDataPage();
    };
    //获取分页数据
    const getDataPage = () => {
      request
        .request({
          url: "/article/getAllArticleByPage",
          params: {
            title: query.title,
            currentPage: query.currentPage,
            pageSize: query.pageSize,
          },
          method: "get",
        })
        .then((res) => {
          //获取当前页的数据
          tableData.value = res.data.data.rows;
          //获取总记录数
          totalCount.value = res.data.data.count;
        })
        .catch((e) => {
          console.log(e);
        });
    };
    getDataPage();
    // 分页导航
    const handlePageChange = (val) => {
      query.currentPage = val;
      getDataPage();
    };
    // 删除操作
    const handleDelete = (index, row) => {
      request
        .request({
          url: "/article/delarticle",
          method: "get",
          params: {
            articleId: row.articleId,
          },
        })
        .then((res) => {
          if (res.data.msg == "success") {
            getDataPage();
            ElNotification({
              title: "删除成功",
              message: "成功删除用户信息",
            });
          } else {
            ElNotification({
              title: "删除失败",
              message: "请联系管理员检查失败原因",
            });
          }
        })
        .catch((e) => {
          console.log(e);
        });
    };
    //修改操作
    const handleEdit = (index, row) => {
      //点击后查询点击用户信息并展示
      // getArticle(row);
      ruleForm.articleId = row.articleId;
      ruleForm.content = row.content;
      ruleForm.title = row.title;
      ruleForm.desc = row.desc;
      ruleForm.articleCategory = row.articleCategory;
      ruleForm.titleImageUrl = row.titleImageUrl;
      // console.log(row.articleId, "查询后的数据", ruleForm);
      dialogVisible.value = true;
    };
    const handleClose = (done) => {
      ElMessageBox.confirm("Are you sure to close this dialog?")
        .then(() => {
          done();
        })
        .catch(() => {
          // catch error
        });
    };
    // 获取指定文章数据
    const getArticle = (row) => {
      ruleForm.articleId = row.articleId;
      ruleForm.content = row.content;
      ruleForm.title = row.title;
      ruleForm.desc = row.desc;
      ruleForm.articleCategory = row.articleCategory;
      ruleForm.titleImageUrl = row.titleImageUrl;
    };
    //修改
    const updateArticle = (row) => {
      request
        .request({
          url: "/article/updateArticle",
          method: "post",
          data: {
            articleId: ruleForm.articleId,
            title: ruleForm.title,
            desc: ruleForm.desc,
            articleCategory: ruleForm.articleCategory,
            titleImageUrl: ruleForm.titleImageUrl,
            content: ruleForm.content,
          },
        })
        .then((res) => {
          if (res.data.msg == "success") {
            getDataPage();
            console.log("成功");
          } else {
            console.log("失败");
          }
        })
        .catch((e) => {
          console.log(e);
        });
    };
    return {
      query,
      totalCount,
      tableData,
      dialogVisible,
      ruleForm,
      handleSearch,
      handlePageChange,
      handleDelete,
      handleEdit,
      handleClose,
      getDataPage,
      getArticle,
      updateArticle,
      ruleFormRef,
    };
  },
};
</script>

<style >
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}

.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>